<?php
declare (strict_types = 1);

namespace app\admin\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class SystemRole extends Model
{
    protected $pk = "role_id";


    /**
     * 获取身份
     * @param $roleId
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getRoleById($roleId){
        return $this->where('role_id',$roleId)->find();
    }
    /**
     * 获取管理员身份列表
     * @param $roleIds
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getRoleListByIdIn($roleIds){
        return $this->whereIn('role_id',$roleIds)->select()->toArray();
    }

    /**
     * 获取所有身份列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getList(){
        return $this->where('status',1)->select()->toArray();
    }

    /**
     * 身份列表
     * @param $getData
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getAdminRoleList($getData,$page,$limit){
        $count = $this->count();
        $statusType = [0=>'隐藏',1=>'显示'];
        $systemMenuModel = new SystemMenu();
        $getMenuList = $systemMenuModel->getList([],'menu_id,menu_name');
        $menuList = [];
        foreach ($getMenuList as $key=>$value){
            $menuList[$value['menu_id']] = $value['menu_name'];
        }
        $data = $this->page($page,$limit)->order('role_id','desc')->select()
            ->each(function ($item)use($statusType,$menuList){
            $item['status_name'] = $statusType[$item['status']];
            if($item['rules'] == 'all'){
                $item['rules_name'] = '所有权限';
            }else if($item['rules']){
                $roleMenu = explode(",",$item['rules']);
                $menuListName = [];
                foreach ($roleMenu as $key=>$value){
                    if(isset($menuList[$value])){
                        $menuListName[] = $menuList[$value];
                    }
                }
                $item['rules_name'] = implode(",",$menuListName);
            }
            return $item;
        })->toArray();
        return compact('count','data');
    }

    /**
     * 添加身份
     * @param $addData
     * @return int|string
     */
    function addData($addData){
        return $this->insertGetId([
            'role_name'=>$addData['role_name'],
            'status'=>$addData['status'],
            'rules'=>implode(",",$addData['rules']),
            'create_time'=>date("Y-m-d H:i:s"),
            'update_time'=>date("Y-m-d H:i:s"),
        ]);
    }

    /**
     * 修改身份
     * @param $saveData
     * @return int|string
     */
    function editData($saveData){
        $updateData = [
            'role_name'=>$saveData['role_name'],
            'rules'=>implode(",",$saveData['rules']),
            'update_time'=>date("Y-m-d H:i:s"),
        ];
        return $this->where('role_id',$saveData['role_id'])->update($updateData);
    }

    /**
     * 获取用户权限id
     * @param $roleIds
     * @return array
     */
    function getRolePowerIdList($roleIds){
        $powerIdList = $this->whereIn('role_id',$roleIds)->column('rules');
        $adminPower = '';
        foreach ($powerIdList as $key=>$value){
            $adminPower = $adminPower.",".$value;
        }
        $powerList = explode(",",$adminPower);
        return array_unique($powerList);
    }

}
